package jp.co.ricoh.melody.views.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import java.io.File;
import jp.co.ricoh.melody.R;
import jp.co.ricoh.melody.models.entities.SoundData;
import jp.co.ricoh.melody.presenters.ISoundListDialogFragmentPresenter;
import jp.co.ricoh.melody.presenters.SoundListDialogFragmentPresenter;
import jp.co.ricoh.melody.utils.ConstantUtils;
import jp.co.ricoh.melody.utils.HDDUtil;
import jp.co.ricoh.melody.views.adapters.SDCardSoundRecycleView;

/**
 * Created by tanyajie on 2016/11/17.
 */

public class SDCardSoundListDialogFragment extends DialogFragment implements View.OnClickListener {
    /**
     * This view is used to display list sounds.
     */
    private SDCardSoundRecycleView mRecyclerView;
    /**
     * This button is used to select sound for melody and dismiss dialog.
     */
    private Button mOkBtn;
    /**
     * This button is used to dismiss dialog.
     */
    private Button mCancelBtn;

    /**
     * Presenter of this view.
     */
    private ISoundListDialogFragmentPresenter mISoundListDialogFragmentPresenter;

    private SoundData mSoundData;

    private static final String ARG_SOUND_DATA = "ARG_SOUND_DATA";

    public SDCardSoundListDialogFragment() {
    }

    public interface OnSoundDialogDismissListener {
        void onDismiss(SoundData soundData);
    }

    public static SDCardSoundListDialogFragment newInstance(SoundData soundData) {
        SDCardSoundListDialogFragment sdCardSoundListDialogFragment = new SDCardSoundListDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ARG_SOUND_DATA, soundData);
        sdCardSoundListDialogFragment.setArguments(bundle);
        return sdCardSoundListDialogFragment;
    }

    public void setOnDismissListener(SoundListDialogFragment.OnSoundDialogDismissListener onSoundDialogDismissListener) {
        mISoundListDialogFragmentPresenter = new SoundListDialogFragmentPresenter();
        mISoundListDialogFragmentPresenter.onCreate(getActivity(), onSoundDialogDismissListener);
    }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        // Set dialog fragment without title.
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        // Set size for dialog.
        if (dialog != null) {
            dialog.getWindow().setLayout(getResources().getDimensionPixelOffset(R.dimen.sound_pciker_dialog_width),
                    getResources().getDimensionPixelOffset(R.dimen.sound_pciker_dialog_height));
        }
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dialog_sound_sdcard, container, false);
        initView(view);
        return view;
    }

    /**
     * This method is used to initialize view.
     *
     * @param view
     */
    private void initView(View view) {
        mRecyclerView = (SDCardSoundRecycleView) view.findViewById(R.id.fragment_dialog_sd_card_sound_picker_recyclerview);
        mRecyclerView.onCreate(getActivity(), mSoundData);
        mOkBtn = (Button) view.findViewById(R.id.fragment_dialog_sound_btn_ok);
        mOkBtn.setOnClickListener(this);
        mCancelBtn = (Button) view.findViewById(R.id.fragment_dialog_sound_btn_cancel);
        mCancelBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fragment_dialog_sound_btn_ok:
                final SoundData soundData = mRecyclerView.getSelectedItem();
                if(soundData != null){
                    final AlertDialog dialog = new AlertDialog.Builder(view.getContext()).setMessage(R.string.sound_picker_dialog_sdcard_wait).show();
                    new AsyncTask<Void,Void,Boolean>(){

                        @Override
                        protected Boolean doInBackground(Void... params) {
                            File soundFile = new File(soundData.getSoundUri());
                            File file = new File(ConstantUtils.HDD_SAVE_PAHT + soundFile.getName());
                            if (file.exists()) {
                                file.delete();
                            }
                            return HDDUtil.copySdcardFile(soundData.getSoundUri(), file.getAbsolutePath());
                        }
                        @Override
                        protected void onPostExecute(Boolean aBoolean) {
                            super.onPostExecute(aBoolean);
                            dialog.dismiss();
                            dismissAllowingStateLoss();
                            mISoundListDialogFragmentPresenter.onCompletedSelectSound(soundData);
                        }
                    }.execute();
                }
                break;
            case R.id.fragment_dialog_sound_btn_cancel:
                dismissAllowingStateLoss();
                break;
            default:
                break;
        }
    }
    @Override
    public void onDismiss(DialogInterface dialog) {
        mRecyclerView.onDestroyView();
        super.onDismiss(dialog);
    }
}
